package com.qc.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import com.qc.sys.dao.SysMenuDao;
import com.qc.sys.dao.SysRoleDao;
import com.qc.sys.entity.SysMenu;
import com.qc.sys.service.SysMenuService;
import com.qc.sys.vo.MenuVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * (SysMenu)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-08-26 10:30:22
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuDao menuDao;

    @Autowired
    private SysRoleDao roleDao;

    @Override
    public List<SysMenu> queryAllMenuForListByUserId(Integer id) {
        //根据userid查询角色id的集合
        List<Integer> roleIds=this.roleDao.queryRoleIdsByUserId(id);
        //根据角色ID的集合，查询菜单的ID的集合
        if(null!=roleIds&&roleIds.size()>0){
            List<Integer> menuIds=this.roleDao.queryMenuIdsByRids(roleIds);
            if(null!=menuIds&&menuIds.size()>0) {
                QueryWrapper<SysMenu> qw = new QueryWrapper<>();
                qw.eq("available", Constant.USER_AVAILABLE_TRUE);
                qw.and(new Consumer<QueryWrapper<SysMenu>>() {
                    @Override
                    public void accept(QueryWrapper<SysMenu> menuQueryWrapper) {
                        menuQueryWrapper.eq("type", Constant.MENU_TYPE_TOP)
                                .or().eq("type", Constant.MENU_TYPE_LEFT);
                    }
                });
                qw.in("id",menuIds);
                qw.orderByAsc("ordernum");
                List<SysMenu> menus=this.menuDao.selectList(qw);
                return menus;
            }else{
                return new ArrayList<>();
            }
        }else{
            return new ArrayList<>();
        }
    }


    @Override
    public List<SysMenu> queryAllMenuForList() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available", Constant.USER_AVAILABLE_TRUE);
        queryWrapper.and(new Consumer<QueryWrapper<SysMenu>>() {
            @Override
            public void accept(QueryWrapper<SysMenu> sysMenuQueryWrapper) {
                sysMenuQueryWrapper.eq("type",Constant.MENU_TYPE_TOP)
                        .or().eq("type",Constant.MENU_TYPE_LEFT);
            }
        });
        queryWrapper.orderByAsc("ordernum");
        return this.menuDao.selectList(queryWrapper);
    }

    @Override
    public DataGridView loadAllMenu(MenuVo menuVo) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(menuVo.getAvailable()!=null,"available",menuVo.getAvailable());
        queryWrapper.orderByAsc("ordernum");
        List<SysMenu> menuList = this.menuDao.selectList(queryWrapper);
        return new DataGridView(Long.valueOf(menuList.size()),menuList);
    }

    @Override
    public Integer queryMenuMaxOrderNum() {
        return this.menuDao.queryMenuMaxOrderNum();
    }

    @Override
    public ResultObj saveMenu(SysMenu menu) {
        try {
            if(menu.getType().equals("topmenu")){
                menu.setPid(0);
            }
            menu.setSpread(Constant.SPREAD_FALSE);
            menu.setAvailable(Constant.USER_AVAILABLE_TRUE);
            this.menuDao.insert(menu);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @Override
    public DataGridView queryMenuById(Integer id) {
        SysMenu menu = this.menuDao.selectById(id);
        return new DataGridView(menu);
    }

    @Override
    public ResultObj updateMenu(SysMenu menu) {
        try {
            this.menuDao.updateById(menu);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @Override
    public Integer queryMenuChildrenCountById(Integer id) {
        return this.menuDao.queryMenuChildrenCount(id);
    }

    @Override
    public ResultObj deleteMenu(Integer id) {
        try {
            this.menuDao.deleteById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_SUCCESS;
        }
    }

    @Override
    public List<String> queryPermissionCodesByUserId(Integer id) {
        //根据userid查询角色id的集合
        List<Integer> roleIds=this.roleDao.queryRoleIdsByUserId(id);

        //根据角色ID的集合，查询菜单的ID的集合
        if(null!=roleIds&&roleIds.size()>0){
            List<Integer> menuIds=this.roleDao.queryMenuIdsByRids(roleIds);
            if(null!=menuIds&&menuIds.size()>0) {
                QueryWrapper<SysMenu> qw = new QueryWrapper<>();
                qw.eq("available", Constant.USER_AVAILABLE_TRUE);
                qw.eq("type",Constant.MENU_TYPE_PERMISSION);
                qw.in("id",menuIds);
                qw.orderByAsc("ordernum");
                List<SysMenu> menus=this.menuDao.selectList(qw);
                List<String> permissions=new ArrayList<>();
                for (SysMenu menu : menus) {
                    permissions.add(menu.getTypecode());
                }
                return permissions;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
}